package com.mrzhou.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mrzhou.common.entity.system.User;
import com.mrzhou.common.entity.system.UserInfo;
import com.mrzhou.common.feign.client.ISystemClient;
import com.mrzhou.core.tool.api.R;
import com.mrzhou.system.cache.SystemCache;
import com.mrzhou.user.mapper.UserMapper;
import com.mrzhou.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private ISystemClient systemClient;

    @Override
    public R<UserInfo> userInfo(String account) {
        User user = this.baseMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getAccount, account));
        if(null == user) {
            return R.fail("用户不存在, 请检查账号信息或注册！");
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setUser(user);
        userInfo.setRoleAlias(SystemCache.getRoleAlias(user.getRoleId()));
        return R.data(userInfo);
    }

    @Override
    public R<UserInfo> userInfoById(Long userId) {
        User user = this.getById(userId);
        if(null == user) {
            return R.fail("用户不存在, 请检查账号信息或注册！");
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setUser(user);
        userInfo.setRoleAlias(SystemCache.getRoleAlias(user.getRoleId()));
        return R.data(userInfo);
    }

}
